<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Sidecar</title>
    <style>
      .container {
        display: flex;
      }
      .container > div {
        flex: 1;
      }
    </style>
  </head>

  <body>
    <div class="container">
      <div id="backend"></div>
      <div id="frontend"></div>
    </div>
    <script>
      function addMessage(div, message) {
        const p = document.createElement('p')
        p.innerText = message
        div.appendChild(p)
      }

      const backendDiv = document.getElementById('backend')
      window.__TAURI__.event.listen('message', (event) => {
        addMessage(backendDiv, event.payload)
      })

      const frontendDiv = document.getElementById('frontend')
      const { Command } = window.__TAURI__.shell
      const command = Command.sidecar('binaries/app')
      command.on('close', (data) => {
        addMessage(
          frontendDiv,
          `command finished with code ${data.code} and signal ${data.signal}`
        )
      })
      command.on('error', (error) =>
        addMessage(frontendDiv, `command error: "${error}"`)
      )
      command.stdout.on('data', (line) =>
        addMessage(frontendDiv, `command stdout: "${line}"`)
      )
      command.stderr.on('data', (line) =>
        addMessage(frontendDiv, `command stderr: "${line}"`)
      )
      command.spawn()
    </script>
  </body>
</html>
